<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Session;

class Admin extends Model
{
    // 登录验证
    public function checkLogin($req)
    {
        $p = $req->post();
        $name = $p['username'];
        $pwd = md5($p['pass']);

        if (is_null($pwd)) {
            return '密码不可以为空';
        }

        $res = db('lamp_user')
            ->field(['id', 'username'])
            ->where('username', $name)
            ->where('userpass', $pwd)
//            ->where('status','1')
            ->find();


        if ($res) {

            // 查询该用户相关权限
            $data = db('lamp_node')
                ->field('mname,aname')
                ->where('id in' . db('lamp_role_node')
                        ->field('nid')
                        ->where('rid in' . db('lamp_user_role')
                                ->field('rid')
                                ->where('uid', $res['id'])
                                ->buildSql())
                        ->buildSql())
                ->select();
            // var_dump($data);die;
            $arr = array();
            $arr['Index'] = array('index');
            foreach ($data as $k => $v) {
                $arr[$v['mname']][] = $v['aname'];
            }
            // var_dump($arr);die;
            // 将权限对应的控制器、方法的节点存入session
            session::set('admin_node', $arr);
            // unset($_SESSION['think']);
            // var_dump($_SESSION);die;

            return $res;

        } else {
            return '用户名或密码不匹配！';
        }
    }

    public function findAll()
    {
        $list = Db::name('lamp_user')->field(['id', 'username', 'name'])->select();
        return $list;
    }

    public function selectroel($id)
    {
        $res = db('lamp_user_role')->field('rid')->where('uid', $id)->find();
        return $res;
    }


}
